﻿using Newtonsoft.Json;

namespace CTP.CRM.Core.JsonSerialization
{
    //
    // 摘要:
    //     返回时long转字符串
    public class LongJsonConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(long) || objectType == typeof(long?);
        }

        public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
        {
            if (reader.Value == null || string.IsNullOrEmpty(reader.Value.ToString()))
            {
                return null;
            }

            long result;
            return long.TryParse(reader.Value.ToString(), out result) ? ((object)result) : ((object)default(DateTime));
        }

        public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
        {
            writer.WriteValue(((long)value).ToString());
        }
    }
}
